package com.svo.weitu.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.svo.weitu.model.db.DbHelper;
import com.svo.weitu.model.entity.ListEntity;

/**
 * 收藏业务类
 * @author Administrator
 *
 */
public class MCol {
	private DbHelper dbHelper;
	private Context context;
	public MCol(Context context){
		this.context = context;
		dbHelper=DbHelper.getInstance(context);
	}
	public void insert(ListEntity entity) {
		try {
			ContentValues values = new ContentValues();
			values.put("_id", entity.getId());
			values.put("title", entity.getTitle());
			values.put("description", entity.getDesc());
			values.put("insert_time", System.currentTimeMillis());
			values.put("thumbnail", entity.getThumbnail());
			values.put("urls", entity.getUrls());
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			db.insert("col", null, values);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void del(int id) {
		try {
			SQLiteDatabase db = dbHelper.getWritableDatabase();
			db.execSQL("delete from col where _id = "+id);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public ArrayList<ListEntity> query() {
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from col order by insert_time desc";
		Cursor cursor = db.rawQuery(sql,null);
		ArrayList<ListEntity> entities = new ArrayList<ListEntity>();
		ListEntity entity;
		while (cursor.moveToNext()) {
			entity = new ListEntity(context);
			entity.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			entity.setTitle(cursor.getString(cursor.getColumnIndex("title")));
			entity.setDesc(cursor.getString(cursor.getColumnIndex("description")));
			entity.setTime(cursor.getLong(cursor.getColumnIndex("insert_time")));
			entity.setThumbnail(cursor.getString(cursor.getColumnIndex("thumbnail")));
			entity.setUrls(cursor.getString(cursor.getColumnIndex("urls")));
			entities.add(entity);
		}
		return entities;
	}
	public List<Integer> getIds() {
		List<Integer> colList = new Vector<Integer>();
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		String sql = "select * from col order by insert_time desc";
		Cursor cursor = db.rawQuery(sql,null);
		while (cursor.moveToNext()) {
			colList.add(cursor.getInt(cursor.getColumnIndex("_id")));
		}
		return colList;
	}
}
